using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Configuration;
using System.Web.Configuration;
using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace eGov.SharePoint.MyPeeps.WebParts
{
    public class WhatsOnYourMindWebPartEditor : EditorPart
    {
        private TextBox txtSiteLocation;
        private TextBox txtTwitterUser;
        private TextBox txtTwitterPassword;
        private Button btnResetFacebook;
        private Label lblSiteLocation;
        private Label lblTwitterUser;
        private Label lblTwitterPassword;
        protected bool ResetFacebookConnection
        {
            get
            {
                if (ViewState["wpeditor_ResetFacebookConnection"] == null)
                    ViewState["wpeditor_ResetFacebookConnection"] = false;
                return (bool)ViewState["wpeditor_ResetFacebookConnection"];
            }
            set
            {
                ViewState["wpeditor_ResetFacebookConnection"] = value;
            }
        }

        public WhatsOnYourMindWebPartEditor()
        {
            
        }

        protected override void CreateChildControls()
        {
            WhatsOnYourMind webPart = this.WebPartToEdit as WhatsOnYourMind;

            base.CreateChildControls();
            this.txtSiteLocation = new TextBox();
            this.txtTwitterPassword = new TextBox() { TextMode = TextBoxMode.Password };
            this.txtTwitterUser = new TextBox();
            this.lblSiteLocation = new Label() { Text = "Twitter URL" };
            this.lblTwitterPassword = new Label() { Text = "Twitter Password" };
            this.lblTwitterUser = new Label() { Text = "Twitter User Name" };
            this.btnResetFacebook = new Button() { Text = "Reset Facebook Connection" };
            this.btnResetFacebook.Click += new EventHandler(btnResetFacebook_Click);
            //this.btnResetFacebook.OnClientClick = "FB.Connect.logout(function() { reload(); }); return false;";

            this.Controls.Add(this.lblSiteLocation);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(this.txtSiteLocation);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(this.lblTwitterUser);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(this.txtTwitterUser);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(this.lblTwitterPassword);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(this.txtTwitterPassword);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(this.btnResetFacebook);
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(new LiteralControl("<a href='#' onclick='FB.Connect.logout(function() { reload(); }); return false;' ><img id='fb_logout_image' src='http://static.ak.fbcdn.net/images/fbconnect/logout-buttons/logout_small.gif' alt='Connect'/></a>"));
            this.Controls.Add(new LiteralControl("<br/>"));
            this.Controls.Add(new LiteralControl("<fb:login-button autologoutlink='true' size='medium' background='white' length='short'></fb:login-button>"));
            this.Controls.Add(new LiteralControl("<br/>"));
        }

        protected void btnResetFacebook_Click(object sender, EventArgs e)
        {
            this.ResetFacebookConnection = true;
        }

        public override bool ApplyChanges()
        {
            this.EnsureChildControls();

            WhatsOnYourMind webPart = this.WebPartToEdit as WhatsOnYourMind;

            webPart.PeepSite = this.txtSiteLocation.Text;
            webPart.PeepUser = this.txtTwitterUser.Text;
            webPart.PeepPassword = this.txtTwitterPassword.Text;

            if (this.ResetFacebookConnection == true)
            {
                webPart.LogoutActiveFBSession();
                webPart.FBSessionKey = String.Empty;
            }

            return true;
        }

        public override void SyncChanges()
        {
            this.EnsureChildControls();

            WhatsOnYourMind webPart = this.WebPartToEdit as WhatsOnYourMind;
            this.txtSiteLocation.Text = webPart.PeepSite;
            this.txtTwitterPassword.Text = webPart.PeepPassword;
            this.txtTwitterPassword.TextMode = TextBoxMode.Password;
            this.txtTwitterUser.Text = webPart.PeepUser;

            if (String.IsNullOrEmpty(webPart.FBSessionKey))
                this.btnResetFacebook.Visible = false;

        }
    }
}
